package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.PlantEncyclopediaActivity;
import com.ihighest.app.infox.activity.WikiContentListActivity;
import com.ihighest.app.infox.fragment.PlantFragment;

/**
 * 种植百科左侧list数据
 * 
 * @author Vity Created at 2015-10-21 下午5:43:28
 * @version 1.0
 */
@SuppressLint("InflateParams")
public class PlantLeftListViewAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	Context context;
	JSONArray array;
	int selectedPosition = -1;// 选中的位置
	static String TAG = "PlantLeftListViewAdapter";

	public PlantLeftListViewAdapter(Context context, JSONArray array) {
		this.context = context;
		this.array = array;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return array != null ? array.length() : 0;
	}

	public void setSelectedPosition(int selectedPosition) {
		this.selectedPosition = selectedPosition;
	}

	@Override
	public Object getItem(int arg0) {
		try {
			return array.get(arg0);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_wiki_left_class_item, null);
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.view_seq = convertView.findViewById(R.id.view_seq);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		try {
			holder.tv_title.setText(array.getJSONObject(position).getString("title"));
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage(), e);
		}
		holder.tv_title.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				try {
					if (array.getJSONObject(position).getBoolean("leaf")) {
						Intent intent = new Intent(context, WikiContentListActivity.class);
						intent.putExtra("id", array.getJSONObject(position).getString("id"));
						context.startActivity(intent);
					} else {
						// 跳转fragment
						PlantFragment fragment = new PlantFragment(context, array.getJSONObject(position).getJSONArray("sub"));
						((PlantEncyclopediaActivity) context).getSupportFragmentManager().beginTransaction()
								.add(R.id.id_plant_framelayout, fragment).commit();
					}
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);
				}
				setSelectedPosition(position);
				notifyDataSetInvalidated();
			}
		});
		if ((selectedPosition == position) || (selectedPosition == -1 && position == 0)) {
			convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
			holder.view_seq.setBackgroundColor(Color.parseColor("#EB6100"));
		} else {
			convertView.setBackgroundColor(Color.parseColor("#F0F0F0"));
			holder.view_seq.setBackgroundColor(Color.parseColor("#F0F0F0"));
		}
		return convertView;
	}

	class ViewHolder {
		TextView tv_title;
		View view_seq;
	}
}
